using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MIR
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Form2 f2 = new Form2();
            f2.Show(this);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.ShowDialog();
            tbFile.Text = folderBrowserDialog.SelectedPath;
            
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(tbFile.Text);
                toolStripProgressBar1.Maximum = dir.GetFiles().Length;
                toolStripProgressBar1.Step = 1;

                foreach (FileInfo fileInfo in dir.GetFiles())
                {
                    string fileExt = fileInfo.Extension.ToLower();
                    ImageType imageType;
                    if (fileExt == ".jpg" || fileExt == ".jpeg")
                        imageType = ImageType.jpg;
                    else if (fileExt == ".gif")
                        imageType = ImageType.gif;
                    else if (fileExt == ".png")
                        imageType = ImageType.png;
                    else if (fileExt == ".bmp")
                        imageType = ImageType.bmp;
                    else
                    {
                        toolStripProgressBar1.PerformStep();
                        continue; //skip unknown files
                    }
                    Image img = Image.FromFile(fileInfo.FullName);
                    Save(imageType,
                         img,
                         fileInfo,
                         tbSuffix.Text,
                         (int)numericUpDownW.Value,
                         (int)numericUpDownH.Value,
                         cbKeepRatio.Checked);
                    toolStripProgressBar1.PerformStep();
                }
            }
            catch
            {
                return ;
            }
        }

        private void cbKeepRatio_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownH.Enabled = !cbKeepRatio.Checked;
        }

        /// <summary>
        /// Save image to a file
        /// </summary>
        /// <param name="img"></param>
        /// <param name="fileName"></param>
        private void Save(ImageType imageType, Image img, FileInfo fileInfo, string fileSuffix, int newW, int newH, bool keepRatio)
        {
            string fileExt = fileInfo.Extension.ToLower();
            System.Drawing.Image imgOriginal;
            System.Drawing.Image imgThumbnail;
            imgOriginal = img;
            imgThumbnail = ImageUtil.ResizeImage(imgOriginal, newW, newH, keepRatio);
            if (imgThumbnail == null)
                return;
            MemoryStream streamThumbnail = new MemoryStream();
            switch (imageType)
            {
                case ImageType.jpg:
                    imgThumbnail.Save(streamThumbnail, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case ImageType.bmp:
                    imgThumbnail.Save(streamThumbnail, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case ImageType.gif:
                    imgThumbnail.Save(streamThumbnail, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                case ImageType.png:
                    imgThumbnail.Save(streamThumbnail, System.Drawing.Imaging.ImageFormat.Png);
                    break;
            }
            byte[] thumbnailImageContent = new Byte[streamThumbnail.Length];
            streamThumbnail.Position = 0;
            streamThumbnail.Read(thumbnailImageContent, 0, (int)streamThumbnail.Length);

            string newFullName = fileInfo.Directory + "\\" + fileInfo.Name.Substring(0,fileInfo.Name.IndexOf('.')) + fileSuffix + fileInfo.Extension;
            FileStream fileStream = new FileStream(newFullName, FileMode.Create);
            fileStream.Write(thumbnailImageContent, 0, thumbnailImageContent.Length);
            fileStream.Close();

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.immensetech.com");
        }


    }
}